package cn.jdy.plugins.wap;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

import javax.xml.crypto.Data;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.jdy.authority.entity.Organ;
import cn.jdy.authority.service.OrganService;
import cn.jdy.common.ctrl.BaseCtrl;
import cn.jdy.common.util.WxHttpUtil;
import cn.jdy.common.util.XMLUtil;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.framework.util.DateUtil;
import cn.jdy.framework.util.StringUtil;
import cn.jdy.platform.service.ConfService;
import cn.jdy.plugins.entity.CRateOrder;
import cn.jdy.plugins.entity.Maid;
import cn.jdy.plugins.entity.Maidjqfhcdb;
import cn.jdy.plugins.entity.Mall;
import cn.jdy.plugins.entity.Member;
import cn.jdy.plugins.entity.PointRecord;
import cn.jdy.plugins.entity.Productx;
import cn.jdy.plugins.service.CRateOrderService;
import cn.jdy.plugins.service.MaidService;
import cn.jdy.plugins.service.MaidjqfhcdbService;
import cn.jdy.plugins.service.MallService;
import cn.jdy.plugins.service.MemberService;
import cn.jdy.plugins.service.PointRecordService;
import cn.jdy.plugins.service.ProductxService;
import cn.jdy.plugins.wap.util.ChatUtil;
import cn.jdy.plugins.wap.util.RedPacketUtil;


/**
 * @title 旅居订单管理操作
 * @author jga
 * @since 2018-09-21
 */
@Controller
@RequestMapping("/cRateOrder")
public class CRateOrderCtrl extends BaseCtrl{
	@Autowired
	private CRateOrderService cRateOrderService;
	@Autowired
	private ConfService confService;
	//默认列表路径
	private static String viewName = "redirect:/CRateOrder/list?s=";
	
    //回调地址  
    private static String notify_url = "http://kylj.kylj365.com/w/cRateOrder/weixinNotify"; //测试  
	@Autowired
	private ProductxService productxService;
	@Autowired
	private MallService mallService;
	@Autowired
	private OrganService organService;
	@Autowired
	private MemberService memberService;
	@Autowired
	private PointRecordService pointRecordService;
	
	@Autowired
	private MaidService maidService;
	@Autowired
	private MaidjqfhcdbService maidjqfhcdbService;
	
	/**
	 * 客户自选去订单
	 * @param id
	 * @return
	 */
	@RequestMapping("/editPr/{id}.html")
	public ModelAndView editPr(@PathVariable Long id) {
		ModelAndView mv = new ModelAndView("/order/order.jsp");
		mv.addObject("data", id==null||id==0 ? id : this.productxService.find(id));
		return mv;
	}
	
	/**
	 * 后台定好去订单
	 * @param id
	 * @return
	 */
	@RequestMapping("/editdihPr/{id}.html")
	public ModelAndView editdihPr(@PathVariable Long id) {
		ModelAndView mv = new ModelAndView("/order/dinghaoorder.jsp");
		Productx px=this.productxService.find(id);
		 Date d=new Date();
	     SimpleDateFormat s=new SimpleDateFormat("yyyy-MM-dd");
	     Calendar c = Calendar.getInstance();
	     c.setTime(d);
	     c.add(Calendar.DATE, Integer.valueOf(px.getRefund())+1);//的时间
		mv.addObject("datime",s.format(c.getTime()));//到期时间
		mv.addObject("data", id==null||id==0 ? id : px);
		 Member mbo1 =(Member) session.getAttribute("user");
		CRateOrder co=new CRateOrder();
		co.setMemberid(mbo1.getId());
		mv.addObject("isfirst",cRateOrderService.count(co));//到期时间
	
		 
		return mv;
	}
	
	 
	 /**
     * 微信统一下单接口
     * @param 
     * @param 
     * @param  
     * @param 
     * @param
     * @param 
     * @param 
     * @return
     */
    @RequestMapping("wxprepaydo") 
    @ResponseBody
    public Map wxprepaydo( CRateOrder cp,
    		@RequestParam("mid") String mid
    	  ) { 
    	
    	
    	SortedMap<Object, Object> payMap = new TreeMap<Object, Object>();
    	String result="ok";
	    try {
	          String time_start = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
	      	  Calendar ca = Calendar.getInstance();
	    	  ca.setTime(new Date());
	    	  ca.add(Calendar.DATE, 1);
	    	  String time_expire = new SimpleDateFormat("yyyyMMddHHmmss").format(ca.getTime());
	  			Organ organ = organService.find("40288119669feb25016717b91a6a0001");
	  			
	   /* 	String appid="wxcd9ce5b18fd4acd0";
	    	String mchid="1513139041";
	    	String apikey="qau44x04phduq4niid5ebx3uacbm3n2i";*/
	    	
	    	String appid=organ.getAppId();
	    	String mchid=organ.getMchid();
	    	String apikey=organ.getApikey();
	    	
	    	//qau44x04phduq4niid5ebx3uacbm3n2i
	    	 Member mbo1 =(Member) session.getAttribute("user");
	    	Member mbo=memberService.find(mbo1.getId());
	    	  //确定订单
	    		cp.setMch_id(mchid);
	    		cp.setMemberid(mbo.getId());
	    		cp.setUsername(mbo.getUserName());
	    		cp.setZfStatus("0");
	    		cp.setSendStatus("1");
	          String out_trade_no = readyOrder(cp); 
	          
	          
	          Mall  ma= mallService.find(Integer.valueOf(mid));
	          Organ  og=organService.find(ma.getOrganid());//或者机构的支付信息
	      	  SortedMap<Object, Object> paraMap = new TreeMap<Object, Object>();

	  			
				   /* 	String appid="wxcd9ce5b18fd4acd0";
				    	String mchid="1513139041";
				    	String apikey="qau44x04phduq4niid5ebx3uacbm3n2i";*/
				    	
				
	          paraMap.put("appid", appid); 
	          paraMap.put("mch_id", mchid); 
	          paraMap.put("nonce_str", RedPacketUtil.pay_nonce_str()); 
	          
	     
	          paraMap.put("body", "商品购买"); 
  	          paraMap.put("attach", "商品购买"); 
	          paraMap.put("out_trade_no", out_trade_no);  
	          paraMap.put("total_fee", RedPacketUtil.get_total_fee(cp.getTotal()));   //钱为分 
	          paraMap.put("spbill_create_ip", RedPacketUtil.getHostAddress());
	          paraMap.put("time_start", time_start);
	          paraMap.put("time_expire", time_expire);
	          
	          String nurl=notify_url+"/"+out_trade_no+".html";
	          paraMap.put("notify_url", nurl); 
	          paraMap.put("trade_type", "JSAPI"); 
	          paraMap.put("openid", mbo.getOpenId()+""); 
	          String sign = RedPacketUtil.createSign("UTF-8", paraMap, apikey);
	          paraMap.put("sign", sign); 
	          BaseLog.e(this.getClass(), "加加--："+paraMap.toString());
	          BaseLog.e(this.getClass(), "加加22："+nurl);
	          //统一下单
	          String url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
	          String requestXML = RedPacketUtil.getRequestXml(paraMap);
	  		  String xmlStr = WxHttpUtil.postData(url, requestXML);
	  		  
	  		  BaseLog.e(this.getClass(), xmlStr.toString());
	          
	          // 预付商品id 
	          String prepay_id = ""; 
	         
	          if(xmlStr.indexOf("SUCCESS") != -1) {   
	        	  Map<String, String> map = XMLUtil.doXMLParse(xmlStr); 
	        	  String return_msg = map.get("return_msg");
	        	  if(StringUtil.isNotEmpty(return_msg) && return_msg.equals("OK")){
	        		  prepay_id = (String) map.get("prepay_id"); 
	        	  }else{
	        		  result = "error";
	        	  }
	          }else{
	        	  result = "error";
	          }
	        
	          if("ok".equals(result)){
	        	  payMap.put("appId", appid); 
	        	  payMap.put("timeStamp", ChatUtil.create_timestamp()); 
	        	  payMap.put("nonceStr", RedPacketUtil.pay_nonce_str()); 
	        	  payMap.put("package", "prepay_id=" + prepay_id); 
	        	  payMap.put("signType", "MD5"); 
	        	 
	        	  String paySign = RedPacketUtil.createSign("UTF-8", payMap, apikey); 
	        	  payMap.put("paySign", paySign); 
	          }
	          payMap.put("prepay_id", prepay_id); 
	          payMap.put("result", result);
	          payMap.put("orderNo", out_trade_no);
		} catch (Exception e) {
			result="error";
			BaseLog.e(this.getClass(), "下单失败！", e);
		}
	    return payMap;
    } 
	
    /**
  	 * 微信回调
  	 * @param request
  	 * @param response
  	 * @return
  	 * @throws Exception
  	 */
  	@RequestMapping("/weixinNotify/{id}.html")
  	public ModelAndView weixinNotify(@PathVariable String id) throws Exception {
  		ModelAndView mv = new ModelAndView();
  	
  	 
  		CRateOrder redData = cRateOrderService.findOne("orderNo",id);
  		Productx px=productxService.find(Long.valueOf(redData.getProductxid()));

  		if(redData.getZfStatus().equals("0")){  //判断是否已经支付，不然会重复回调
  		redData.setProductxname(px.getTitle());
  		redData.setImgUrl(px.getImgUrl());
  	
  		redData.setZfStatus("1");  //支付成功
  		redData.setUpdateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
  		redData.setCreateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
  		cRateOrderService.update(redData);  //支付成功
 
  		px.setSku(px.getSold()==null?0:px.getSold()+1);
  		productxService.update(px);  //库存减一
  		
  		
  		Member	member  =memberService.find(redData.getMemberid());
  		member.setScore(member.getScore()+redData.getTotal());
  		
  		if(px.getRmtype().equals("2")){
  	 
  			if(cRateOrderService.countorderMap(member.getId())<= 0) {
  				
  				if(redData.getTotal()>=158&&redData.getTotal()<200){ //新手第一单加钱
  			  		member.setScore(member.getScore()+8);
  				}	else if(redData.getTotal()>=200&&redData.getTotal()<1000){
  			  		member.setScore(member.getScore()+18);
  				}
  				
  			}
  		}
  		
  		if(px.getRmtype().equals("2")){
  		  	 
  			if(cRateOrderService.countorderMap(member.getId())<= 0) {
  				
  				if(redData.getTotal()>=158&&redData.getTotal()<200){ //新手第一单加钱
  			  		member.setScore(member.getScore()+8);
  				}	else if(redData.getTotal()>=200&&redData.getTotal()<1000){
  			  		member.setScore(member.getScore()+18);
  				}
  				
  			}
  		}
  		
  		if(px.getRmtype().equals("14")){ //判断是否代理
 		  	if(px.getIsdlype().equals("0")){//298
 		  		if(member.getUserLevel().equals("1")||member.getUserLevel().equals("2")||member.getUserLevel().equals("3")){
 		  			member.setUserLevel("3");
 		  		}
 		  		
 		  		
 		  		 if(!StringUtil.isEmpty(member.getSjyqm())){
 		  			  //加上级的
 		  	  		Maid  ma=new Maid();
 		  	  		Member	memberf =memberService.find(member.getFid());
 		  	  		if(memberf!=null){
	 		  	  		ma.setOrderNo(id);
	 		  	  		ma.setUsername(member.getUserName());
	 		  	  		ma.setMch_id(redData.getMch_id());
	 		  	  		ma.setMemberid(redData.getMemberid());
	 		  	  		ma.setProductxid(redData.getProductxid());
	 		  	  		ma.setProductxname(px.getTitle());
	 		  	  		ma.setCreateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
	 		  	  		ma.setTotal(redData.getTotal());
	 		  	  		ma.setFs(redData.getFs());
 		  	  			ma.setWermemberid(memberf.getId());
 		  	  	  		ma.setWerusername(memberf.getUserName());
 		  	  	  		ma.setIsfa("0");
 		  	  	// userLevel;//用户等级   0运营商，1超级会员，2普通会员,3 初级代理，4 高级代理，5是1星运营商，6是2星运营商，7是3星运营商

 		  	  	  		if(memberf.getUserLevel().equals("3")){
 		  	 	  	  		ma.setFendtotal(298*0.3);
 		  	 	  		maidService.save(ma);
 		  	  	  		}else 	if(memberf.getUserLevel().equals("4")){
 		  	  	  		ma.setFendtotal(298*0.4);
 		  	  	  		maidService.save(ma);
 		  	  	  		}else if(memberf.getUserLevel().equals("0")||memberf.getUserLevel().equals("5")||memberf.getUserLevel().equals("6")||memberf.getUserLevel().equals("7")||memberf.getUserLevel().equals("8")||memberf.getUserLevel().equals("9")){
 		  	  	  	  		ma.setFendtotal(298*0.4-(298*0.4*0.1));
 	 		  	  	  		maidService.save(ma);
 		  	  	  		}
 		 
 		  	  	  	
 		  	 	 	memberf.setFans((StringUtil.isEmpty(memberf.getFans())?0:Integer.valueOf(memberf.getFans())+1)+"");//加一个粉丝
 		  	  		if(memberf.getUserLevel().equals("3")){  //根据自身级别
 		  	 		memberf.setMoney(memberf.getMoney()+(298*0.3));
 			  		memberf.setKtxmoney(memberf.getKtxmoney()+(298*0.3));
		  	  	  		}else 	if(memberf.getUserLevel().equals("4")){
		  	  			memberf.setMoney(memberf.getMoney()+(298*0.4));
	 			  		memberf.setKtxmoney(memberf.getKtxmoney()+(298*0.4));
		  	  	  		}else if (memberf.getUserLevel().equals("0")||memberf.getUserLevel().equals("5")||memberf.getUserLevel().equals("6")||memberf.getUserLevel().equals("7")||memberf.getUserLevel().equals("8")||memberf.getUserLevel().equals("9")){
		  	  	  			//如果是运营商
		  	  	  			double de=298*0.4*0.1;
		  	   			memberf.setMoney(memberf.getMoney()+(298*0.4-de));
	 			  		memberf.setKtxmoney(memberf.getKtxmoney()+(298*0.4-de));
	 			  		
	 			  	 	Maidjqfhcdb madd=new  Maidjqfhcdb();
			  		 	 ma.setOrderNo(id);
			  		 	ma.setUsername(memberf.getUserName());
			  		 	ma.setMch_id(redData.getMch_id());
			  		 	ma.setMemberid(memberf.getId());
			  		 	ma.setProductxid(redData.getProductxid());
			  		 	ma.setProductxname(px.getTitle());
			  		 	ma.setCreateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
			  		 	ma.setTotal(de);
		  		 	  	maidjqfhcdbService.save(madd);
		  		 	  	
	 			  		
		  	  	  		}
 		  	
 			  		memberService.update(memberf);
 			  		memberService.addOrder(memberf.getId());//升级
	 		   		 if(!StringUtil.isEmpty(memberf.getSjyqm())){//第三级
		 		   		  //加上级的
		  		  	  		Maid  ma3=new Maid();
		  		  	  		Member	member3 =memberService.find(memberf.getFid());
		  		  	  		ma3.setOrderNo(id);
				  		  	ma3.setUsername(member.getUserName());
				  		  	ma3.setMch_id(redData.getMch_id());
				  		  	ma3.setMemberid(redData.getMemberid());
				  			ma3.setProductxid(redData.getProductxid());
				  			ma3.setProductxname(px.getTitle());
				  			ma3.setCreateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
				  			ma3.setTotal(redData.getTotal());
				  			ma3.setFs(redData.getFs());
		  		  	  		if(member3!=null){
		  		  	  		ma3.setWermemberid(member3.getId());
				  		  	ma3.setWerusername(member3.getUserName());
				  		  	ma3.setIsfa("0");
				  	//	  ，1超级会员，2普通会员 不加钱
				  	  	  if(member3.getUserLevel().equals("3")||member3.getUserLevel().equals("4")){ 
				  		  	ma3.setFendtotal(298*0.2);
		  		  	  	  		maidService.save(ma3);
		  		  		  	  	member3.setFans((StringUtil.isEmpty(member3.getFans())?0:Integer.valueOf(member3.getFans())+1)+"");//加一个粉丝
		  			  	  		  	member3.setMoney(member3.getMoney()+(298*0.2));
			  				  		member3.setKtxmoney(member3.getKtxmoney()+(298*0.2));
			  				  	memberService.update(member3);
			  	  	  		}else if (member3.getUserLevel().equals("0")||member3.getUserLevel().equals("5")||member3.getUserLevel().equals("6")||member3.getUserLevel().equals("7")||member3.getUserLevel().equals("8")||member3.getUserLevel().equals("9")){
		  					//如果是运营商
			  	  			double de=298*0.2*0.1;
			  	    		  	ma3.setFendtotal(298*0.2-de);
		  		  	  	  		maidService.save(ma3);
		  		  		  	  	member3.setFans((StringUtil.isEmpty(member3.getFans())?0:Integer.valueOf(member3.getFans())+1)+"");//加一个粉丝
			  	  	  		member3.setMoney(member3.getMoney()+(298*0.2-de));
			  	  	  		member3.setKtxmoney(member3.getKtxmoney()+(298*0.2-de));
			  	  	  		memberService.update(member3);
		 			  	 	Maidjqfhcdb madd=new  Maidjqfhcdb();
				  		 	 ma.setOrderNo(id);
				  		 	ma.setUsername(member3.getUserName());
				  		 	ma.setMch_id(redData.getMch_id());
				  		 	ma.setMemberid(member3.getId());
				  		 	ma.setProductxid(redData.getProductxid());
				  		 	ma.setProductxname(px.getTitle());
				  		 	ma.setCreateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
				  		 	ma.setTotal(de);
			  		 	  	maidjqfhcdbService.save(madd);
			  		 	  	
		  		  	  	  }  			  		
				  		memberService.addOrder(member3.getId());//升级
		  		  	  		}
	
	 		   		 }
 		  	  		}
	 			/*	Maid  ma3=new Maid();
	 				Member	member3x =memberService.find("402880e66701317a016701b6dc040000");//3星
	 		  		member3x.setMoney(member3x.getMoney()+(298*0.03));
	 		  		memberService.update(member3x);
  		  	  		ma3.setOrderNo(id);
		  		  	ma3.setUsername(member.getUserName());
		  		  	ma3.setMch_id(redData.getMch_id());
		  		  	ma3.setMemberid(redData.getMemberid());
		  			ma3.setProductxid(redData.getProductxid());
		  			ma3.setProductxname(px.getTitle());
		  			ma3.setCreateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
		  			ma3.setTotal(redData.getTotal());
		  			ma3.setFs(redData.getFs());
  		  	  		ma3.setWermemberid(member3x.getId());
		  		  	ma3.setWerusername(member3x.getUserName());
		  		  	ma3.setIsfa("0");
		  		  	ma3.setFendtotal(298*0.03);
  		  	  	  		maidService.save(ma3);*/
 		  		 }

 		
 		  	}else if(px.getIsdlype().equals("1")){//998
 		  		member.setUserLevel("4");
 				 if(!StringUtil.isEmpty(member.getSjyqm())){
		  			  //加上级的
		  	  		Maid  ma=new Maid();
		  	  		Member	memberf =memberService.find(member.getFid());
			  		if(memberf!=null){
			  	  		ma.setOrderNo(id);
			  	  		ma.setUsername(member.getUserName());
			  	  		ma.setMch_id(redData.getMch_id());
			  	  		ma.setMemberid(redData.getMemberid());
			  	  		ma.setProductxid(redData.getProductxid());
			  	  		ma.setProductxname(px.getTitle());
			  	  		ma.setCreateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
			  	  		ma.setTotal(redData.getTotal());
			  	  		ma.setFs(redData.getFs());
		  	  			ma.setWermemberid(memberf.getId());
		  	  	  		ma.setWerusername(memberf.getUserName());
		  	  	  		ma.setIsfa("0");
		  	  	  		if(memberf.getUserLevel().equals("3")){
		  	 	  	  		ma.setFendtotal(980*0.3);
		  	 	  	  		maidService.save(ma);
		  	  	  		}else 	if(memberf.getUserLevel().equals("4")){
		  	  	  		ma.setFendtotal(980*0.4);
		  	  	  			maidService.save(ma);
		  	  	  		}else 	if(memberf.getUserLevel().equals("0")||memberf.getUserLevel().equals("5")||memberf.getUserLevel().equals("6")||memberf.getUserLevel().equals("7")||memberf.getUserLevel().equals("8")||memberf.getUserLevel().equals("9")){
		  	  	  		ma.setFendtotal((980*0.4)-(980*0.4*0.1));
		  	  	  			maidService.save(ma);
		  	  	  		}
		  	  	  	
		  	  	  		
		  	  	 // 	memberf.setFans((StringUtil.isEmpty(memberf.getFans())?0:Integer.valueOf(memberf.getFans())+1)+"");//加一个粉丝
		  	  		if(memberf.getUserLevel().equals("3")){
		  	    		memberf.setMoney(memberf.getMoney()+(980*0.3));
		  	    		memberf.setKtxmoney(memberf.getKtxmoney()+(980*0.3));
		  	    		memberService.update(memberf);
	  	  	  		}else 	if(memberf.getUserLevel().equals("4")){
	  	  	  		memberf.setMoney(memberf.getMoney()+(980*0.4));
	  	  	  		memberf.setKtxmoney(memberf.getKtxmoney()+(980*0.4));
	  	  	  		memberService.update(memberf);
	  	  	  		}else 	if(memberf.getUserLevel().equals("0")||memberf.getUserLevel().equals("5")||memberf.getUserLevel().equals("6")||memberf.getUserLevel().equals("7")||memberf.getUserLevel().equals("8")||memberf.getUserLevel().equals("9")){
	 	  	  			//如果是运营商...........................
		  	  		double de=980*0.4*0.1;
	  	   			memberf.setMoney(memberf.getMoney()+(980*0.4-de));
 			  		memberf.setKtxmoney(memberf.getKtxmoney()+(980*0.4-de));
 			  		
 			  	 	Maidjqfhcdb madd=new  Maidjqfhcdb();
		  		 	 ma.setOrderNo(id);
		  		 	ma.setUsername(memberf.getUserName());
		  		 	ma.setMch_id(redData.getMch_id());
		  		 	ma.setMemberid(memberf.getId());
		  		 	ma.setProductxid(redData.getProductxid());
		  		 	ma.setProductxname(px.getTitle());
		  		 	ma.setCreateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
		  		 	ma.setTotal(de);
	  		 	  	maidjqfhcdbService.save(madd);
	  				memberService.update(memberf);
		  	  	  		}
		  	  	memberService.addOrder(memberf.getId());//升级
		  	  	
	 		   		 if(!StringUtil.isEmpty(memberf.getSjyqm())){//第三级
		 		   		  //加上级的
		  		  	  		Maid  ma3=new Maid();
		  		  	  		Member	member3 =memberService.find(memberf.getFid());
		  		  	  		ma3.setOrderNo(id);
				  		  	ma3.setUsername(member.getUserName());
				  		  	ma3.setMch_id(redData.getMch_id());
				  		  	ma3.setMemberid(redData.getMemberid());
				  			ma3.setProductxid(redData.getProductxid());
				  			ma3.setProductxname(px.getTitle());
				  			ma3.setCreateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
				  			ma3.setTotal(redData.getTotal());
				  			ma3.setFs(redData.getFs());
		  		  	  		if(member3!=null){
		  		  	  		ma3.setWermemberid(member3.getId());
				  		  	ma3.setWerusername(member3.getUserName());
				  		  	ma3.setIsfa("0");
				  		  	//	  ，1超级会员，2普通会员 不加钱
					  	  	  if(member3.getUserLevel().equals("3")||member3.getUserLevel().equals("4")){
					  	  		  ma3.setFendtotal(980*0.2);
		  		  	  	  		maidService.save(ma3);
		  		  	  	  		member3.setFans((StringUtil.isEmpty(member3.getFans())?0:Integer.valueOf(member3.getFans())+1)+"");//加一个粉丝
		  		  	  			member3.setMoney(member3.getMoney()+(980*0.2));
		  				  		member3.setKtxmoney(member3.getKtxmoney()+(980*0.2));
		  				  		memberService.update(member3);
		  			  	  	  }	else if (member3.getUserLevel().equals("0")||member3.getUserLevel().equals("5")||member3.getUserLevel().equals("6")||member3.getUserLevel().equals("7")||member3.getUserLevel().equals("8")||member3.getUserLevel().equals("9")){
				  					//如果是运营商
		  			  	  		  double de=980*0.2*0.1;
				  	    		  	ma3.setFendtotal(980*0.2-de);
			  		  	  	  		maidService.save(ma3);
			  		  		  	  	member3.setFans((StringUtil.isEmpty(member3.getFans())?0:Integer.valueOf(member3.getFans())+1)+"");//加一个粉丝
			  		  		  	  	member3.setMoney(member3.getMoney()+(980*0.2-de));
			  		  		  	  	member3.setKtxmoney(member3.getKtxmoney()+(980*0.2-de));
			  		  		  	  	memberService.update(member3);
				  		  		
			 			  	 	Maidjqfhcdb madd=new  Maidjqfhcdb();
					  		 	 ma.setOrderNo(id);
					  		 	ma.setUsername(member3.getUserName());
					  		 	ma.setMch_id(redData.getMch_id());
					  		 	ma.setMemberid(member3.getId());
					  		 	ma.setProductxid(redData.getProductxid());
					  		 	ma.setProductxname(px.getTitle());
					  		 	ma.setCreateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
					  		 	ma.setTotal(de);
				  		 	  	maidjqfhcdbService.save(madd);
				  		 	  	
			  		  	  	  }  
						  	  	memberService.addOrder(member3.getId());//升级
		  		  	  		}
	 		   		 }
	 		   		 
		  	  	}
	 			/*	Maid  ma3=new Maid();
	 				Member	member3x =memberService.find("402880e66701317a016701b6dc040000");//3星
 		  	  		ma3.setOrderNo(id);
		  		  	ma3.setUsername(member.getUserName());
		  		  	ma3.setMch_id(redData.getMch_id());
		  		  	ma3.setMemberid(redData.getMemberid());
		  			ma3.setProductxid(redData.getProductxid());
		  			ma3.setProductxname(px.getTitle());
		  			ma3.setCreateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
		  			ma3.setTotal(redData.getTotal());
		  			ma3.setFs(redData.getFs());
 		  	  		ma3.setWermemberid(member3x.getId());
		  		  	ma3.setWerusername(member3x.getUserName());
		  		  	ma3.setIsfa("0");
		  		  	ma3.setFendtotal(998*0.03);
 		  	  	  		maidService.save(ma3);
 		  	  	  	member3x.setMoney(member3x.getMoney()+(998*0.03));
 		  	  	memberService.update(member3x);*/
		  		 }

 		  	}
 		  	
 			if(StringUtil.isNotEmpty(member.getTuandui())){ //给运营商加钱
			  	 // 		Member	memberf2 =memberService.find(member.getFid());
			  			String id2=member.getTdlist().split(",")[0];
			  			
			  	  		Member	memberw  =memberService.find(member.getFid());

			  			 if(!id2.equals(member.getFid())&&!id2.equals(memberw.getFid()) ){
			  			Maid  ma3=new Maid();
	  		  	  		Member	member3 =memberService.find(id2);
	  		 	  		if(member3!=null&&member3.getUserLevel().equals("0")||member3.getUserLevel().equals("5")||member3.getUserLevel().equals("6")||member3.getUserLevel().equals("7")||member3.getUserLevel().equals("8")||member3.getUserLevel().equals("9")){//并且是运营商
	  		  	  		ma3.setOrderNo(id);
			  		  	ma3.setUsername(member.getUserName());
			  		  	ma3.setMch_id(redData.getMch_id());
			  		  	ma3.setMemberid(redData.getMemberid());
			  			ma3.setProductxid(redData.getProductxid());
			  			ma3.setProductxname(px.getTitle());
			  			ma3.setCreateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
			  			ma3.setTotal(redData.getTotal());
			  			ma3.setFs(redData.getFs());
	  		  	  		ma3.setWermemberid(member3.getId());
			  		  	ma3.setWerusername(member3.getUserName());
			  		  	ma3.setIsfa("0");
			  		  	double  ta=0;
			  		  	if(px.getIsdlype().equals("0")){
			  		    	ma3.setFendtotal(298*0.2-(298*0.2*0.1));
			  		  	member3.setMoney(member3.getMoney()+(298*0.2-(298*0.2*0.1)));
 				  		member3.setKtxmoney(member3.getKtxmoney()+(298*0.2-(298*0.2*0.1)));
 				  		
 				  		ta=298*0.2*0.1;
	  		 		  	}else if(px.getIsdlype().equals("1")){//998
			  		  	ma3.setFendtotal(980*0.2-(980*0.2*0.1));
			  		  	member3.setMoney(member3.getMoney()+(980*0.2-(980*0.2*0.1)));
 				  		member3.setKtxmoney(member3.getKtxmoney()+(980*0.2-(980*0.2*0.1)));
 				  		ta=980*0.2*0.1;
	  		 		  	}
	  		  	  	  		maidService.save(ma3);
	  		  	  	  		
	  		  	  	 	Maidjqfhcdb ma=new  Maidjqfhcdb();
			  		 	 ma.setOrderNo(id);
			  		 	ma.setUsername(member3.getUserName());
			  		 	ma.setMch_id(redData.getMch_id());
			  		 	ma.setMemberid(member3.getId());
			  		 	ma.setProductxid(redData.getProductxid());
			  		 	ma.setProductxname(px.getTitle());
			  		 	ma.setCreateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
			  		 	ma.setTotal(ta);
		  		 	  	maidjqfhcdbService.save(ma);
		  		 	  	
		  		 	  	
		  		 		memberService.update(member3);//加积分
				  	  	memberService.addOrder(member3.getId());//升级
			  		}
	  	 
	  		 	  	
	  		 	
			  		}
			  		 }
  		}
  		
  	
  		memberService.update(member);//加积分
  		

  		
  	/*	PointRecord pr=new PointRecord();
  		pr.setOrderNo(id); 
  		pr.setUsername(member.getUserName());
  		pr.setMch_id(redData.getMch_id());
  		pr.setMemberid(redData.getMemberid());
  		pr.setProductxid(redData.getProductxid());
  		pr.setProductxname(px.getTitle());
  		pr.setCreateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
  		pr.setRpType("0");
  		pr.setType("0");
  		pr.setTotal(redData.getTotal());
  		pointRecordService.save(pr);//增加积分记录*/
  		
  		/*Maid  ma=new Maid();


  		Member	memberf =memberService.findOne("fid",member.getId());
  		
  		ma.setOrderNo(id);
  		ma.setUsername(member.getUserName());
  		ma.setMch_id(redData.getMch_id());
  		ma.setMemberid(redData.getMemberid());
  		ma.setProductxid(redData.getProductxid());
  		ma.setProductxname(px.getTitle());
  		ma.setCreateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
  		ma.setTotal(redData.getTotal());
  		ma.setFs(redData.getFs());
  		if(memberf!=null){
  			ma.setWermemberid(memberf.getId());
  	  		ma.setWerusername(memberf.getUserName());
  	  		
  	  		//计算分多少佣金
  	  		double zyj=0;
  	  		zyj=redData.getTotal()*0.015/365*Double.valueOf(px.getRefund());
  	  		ma.setIsfa("0");
  	  		ma.setFendtotal(zyj);
  	  		maidService.save(ma);
  		}*/
 
  		}
  		return mv;
  	}
  	
 	
  	 /**
     * 生成红包订单
     * @return
     */
    public String readyOrder(CRateOrder cp){
    	//订单号
    	String orderNo = ChatUtil.generateString(18);
    	cp.setOrderNo(orderNo);
    	cp.setCreateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
     
    	cRateOrderService.save(cp);
    	return cp.getOrderNo();
    }
  public static void main(String[] args) {
		 Date d=new Date();
	     SimpleDateFormat s=new SimpleDateFormat("yyyy-MM-dd");
	     Calendar c = Calendar.getInstance();
	     	c.setTime(d);
	     c.add(Calendar.DATE, Integer.valueOf(1+1));//计算30天后的时间
	     System.out.println(s.format(c.getTime()));
}
  
  /**
	 * @return
	 * @title
	 * @description: 校验是否新手单
	 * @author: wuzhiliang
	 * @date: 2014-03-13 16:53
	 */
	@RequestMapping("/checkUnickorder")
	@ResponseBody
	public Map<String, Object> checkUnickorder() {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
   	 Member mbo1 =(Member) session.getAttribute("user");

		if(cRateOrderService.countorderMap(mbo1.getId()) > 0) {
			jsonMap.put("status", "500");
		} else {
			jsonMap.put("status", "200");
		}
		return jsonMap;
	}
}
